// @ is an alias to /src
import { MixedUtil } from '@/utils'
import ajax from '../../utils/ajax'
import Screenfull from '@/components/Screenfull'
import { Message } from 'element-ui'
export default {
	name: 'Login',
	components: {
		Screenfull
	},
	data() {
		return {
			vo: {
				authcodeUrl: '',
			},
			po: {
				username: '',
				password: '',
				code: '',
			},
			systemName: '',
			sysVersion: '',

			wechatAuth: false,
			partnershipCode: 1,
		}
	},
	created() {
		this.getSystemVersion()

		// console.log(location.search)
		let url = location.search
		if (url.indexOf('code') != -1) {
			this.wechatAuth = true
		}
		if (localStorage.getItem('routepath')== '/observe') {
			this.po={...JSON.parse(localStorage.getItem('loginpo'))}
			this.login()
		}
	},
	mounted() {
		window.tlogin = this.tlogin
		window.login = this.login
		window.setLoginData = this.setLoginData
	},

	methods: {
		changeAuthcode: function () {
			this.vo.authcodeUrl = this.$API.getCaptcha + '?r=' + MixedUtil.getRandInt(1000, 9999)
		},

		// 获取版本号
		getSystemVersion: function () {
			ajax.get({
				url: this.$API.getServer + 1,
				data: {},
				showLoading: false,
				success: (response) => {
					if (response.success) {
						this.changeAuthcode()
						this.sysVersion = response.data.webVersion
						this.systemName = response.data.serverName
						document.title = response.data.serverName // head中的title
					}
				},
				error: function (res) {
					console.log(res)
				},
			})
		},
		//获取partnershipCode
		getLoginPartnershipCode() {
			let url = window.location.href
			let splitParams = url.split('?')[1]
			if (splitParams) {
				this.partnershipCode = splitParams.split('=')[1]
			} else {
				this.partnershipCode = 0
			}
			this.$store.getters.partnershipCode = this.partnershipCode
			console.log('partnerCode', this.partnershipCode)
		},
		login: function () {
			// this.getLoginPartnershipCode()
			this.po.partnershipCode = this.partnershipCode
			let self = this
			// console.log((this.po.weixinOpenidWeb = this.$store.getters.openId))
			if (this.wechatAuth) {
				this.po.weixinOpenidWeb = this.$store.getters.openId
			} else {
				this.po.weixinOpenidWeb = ''
			}
			if (this.check()) {
				console.log('登录参数', self.po)
				ajax.post({
					url: self.$API.loginByName,
					data: self.po,
					showLoading: false,
					success: function (resp) {
						let data = resp.data
						localStorage.setItem('loginpo',JSON.stringify(self.po))
						self.$store.dispatch('user/setUserAction', data)
						self.$store.dispatch('user/LoginTypeActon', 'web')
						if (self.wechatAuth) {
							window.location.href = location.origin + location.pathname + location.hash
						}
						if(localStorage.getItem('routepath')== '/observe')
						{
							self.$router.push({ path: '/observe' })
						}
						else{
							self.$router.push({ path: '/' })
						}
						
						// if(resp.data.manager==0){
						// 	Message({
						// 		message: '该账号没有登陆权限。',
						// 		type: 'error',
						// 	})
						// 	ajax.post({
						// 		url: self.$API.logout,
						// 		data: {},
						// 		success: (response) => {
						// 			setTimeout((
						// 				location.reload())
						// 		, 2000)
						// 		},
						// })
						// }
						// else{
						// 	self.$store.dispatch('user/setUserAction', resp.data)
						// 	self.$store.dispatch('user/LoginTypeActon', 'web')
						// 	if (self.wechatAuth) {
						// 		window.location.href = location.origin + location.pathname + location.hash
						// 	}
						// 	 self.$router.push({ path: '/observe' })
						// 	// self.$router.push({ path: '/' })
						// }

						// if(resp.data.manager!=0){
						// self.$router.push({ path: '/' })
						// }
						// else{
						// self.$router.push({ path: '/realtimecontrol' })
						// }
					},
					error: function (resp) {
						ajax.hintAWhile(resp.message, 'error')
						self.changeAuthcode()
					},
				})
			}
		},

		tlogin: function (data) {
			console.log('调用tlogin')
			let self = this
			let d = {
				branchCenterId: 0,
				md: '701CAD396352B8C8EF1B1C758F337E344D535E03',
				offline: 'Admin',
				r: '146834',
			}
			ajax.post({
				url: self.$API.getToken,
				data: data,
				dataType: 'json',
				showLoading: false,
				success: function (resp) {
					// alert("resp:" + resp + "api:" + (typeof API))
					ajax.post({
						url: self.$API.tlogin,
						data: {
							token: resp.data,
						},
						dataType: 'json',
						success: function (resp) {
							self.$store.dispatch('user/setUserAction', resp.data)
							self.$store.dispatch('user/LoginTypeActon', 'client')
							self.$router.push('/admin')
						},
						error: function (resp) {
							console.log(resp)
						},
					})
				},
				error: function (resp) { },
			})
		},
		wechatLogin() {
			let orgin_url = window.location.origin + window.location.pathname
			const redirect_uri = encodeURIComponent(orgin_url)
			// const appid = 'wx2b89e18fd414dfc3' //本地
			// const appid = 'wxbb3e8b860c99861d' // 测试版
			const appid = process.env.VUE_APP_APPID
			const url =
				'https://open.weixin.qq.com/connect/qrconnect?appid=' +
				appid +
				'&redirect_uri=' +
				redirect_uri +
				'&response_type=code&scope=snsapi_login'
			window.location.href = url
		},
		onClickToWeb() {
			window.location.href = window.location.href.replace('shike3k/user', 'shike3k/web')
		},
		check: function () {
			if (!this.po.username) {
				ajax.hintAWhile('请输入用户名!', 'error')
				return false
			}
			if (!this.po.password) {
				ajax.hintAWhile('请输入密码!', 'error')
				return false
			}
			if (!this.po.code) {
				ajax.hintAWhile('请输入验证码!', 'error')
				return false
			}

			return true
		},
		setLoginData(username, password, code) {
			this.po.username = username
			this.po.password = password
			this.po.code = code
		}
	},
}
